<?php
session_start();
$sid = session_id();

function addCartSes($id, $qty){	
 	if($qty == 0){
		$_SESSION['item_'.$id] = NULL;
		$_SESSION['item_'.$id.'_qty'] = NULL;	
	}else{
		$_SESSION['item_'.$id] = $id;
		$_SESSION['item_'.$id.'_qty'] = $_SESSION['item_'.$id.'_qty']+$qty;	
	}
}
 
 
function cartStatus(){
    $totalItems = 0;
	$sql = "SELECT * FROM items";
	 if ($result=mysql_query($sql)) {
      while ($row=mysql_fetch_array($result)) {
		   if(@$_SESSION['item_'.$row['itemId']] == $row['itemId']){
		   		$totalItems ++;
		   }
	  }
	  if($totalItems <= 0){
		  echo 'Your cart is empty ';
	  }else{
		  echo 'You have '.$totalItems.' items in your cart. ';
	  }
	 }			
	return false;
}
 
function JShowCart(){
	if(isset($_POST['updatecart'])){
		$sql = "SELECT * FROM items ORDER by itemDesc";
		$update = mysql_query($sql);
		while($cartrow_edit = mysql_fetch_array($update)){
			if($_SESSION['item_'.$cartrow_edit['itemId']] == $cartrow_edit['itemId']){
				$_SESSION['item_'.$cartrow_edit['itemId'].'_qty'] = $_POST['item_'.$cartrow_edit['itemId'].'_qty_edit'];
			}
		}
	}
	$sql = "SELECT * FROM items ORDER by itemDesc";
	$result = mysql_query($sql);
	$resultlist = mysql_query($sql);
	$ifhasorder = 0;
	while($cartrow_ = mysql_fetch_array($result)){
		if(@$_SESSION['item_'.$cartrow_['itemId']] == $cartrow_['itemId']){
			$ifhasorder++;
			break;
		}
	}
	
	if($ifhasorder > 0){
?>
		<div class="cart_j">
        	<form method="post">
        	<table width="100%">
            	<tr>
                	<th width="28%" align="left">Product Name</th>
                    <th width="58%" align="left">Product Desc</th>
                    <th width="7%">Qty</th>
                    <?php if(@$_SESSION['salesName']){ ?><th width="7%">Price</th><?php } ?>
                   <?php if(@$_SESSION['salesName']){ ?> <th width="7%">Amount</th><?php } ?>
                    
                </tr>
                <?php 
					$total = 0;
					while($cartrow = mysql_fetch_array($resultlist)){
						
						if(@$_SESSION['item_'.$cartrow['itemId']] == $cartrow['itemId']){
							$total = $total+$cartrow['itemPrice']*@$_SESSION['item_'.$cartrow['itemId'].'_qty'];
				?>
                <tr>
                	<td><a class="updateLink" rel="tooltip" title="Images" data-toggle="modal" href="#imgitemModal_pro_<?php echo $cartrow['itemId']; ?>"><?php echo $cartrow['itemName']; ?></a>
                    <div class="j-modal">
							<div id="imgitemModal_pro_<?php echo $cartrow['itemId']; ?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="width:600px;">
								<div class="modal-header">      
									   
										<div id="productsContainer" style="width:560px;">
											<div class="productTitle">
										
											  <font class="economicaBold" style="color:#00545c"><?php echo $cartrow['itemDesc']; ?></font>	
                                              
											 </div>
                                             
                                              
                                                
                                             <table width="100%" class="popdetails">
                                             	<tr>
                                                	<td width="32%">Wattage:</td>
                                                    <td width="68%"><strong><?php echo $cartrow['wattage']; ?></strong></td>
                                                </tr>
                                                <tr>
                                                	<td>Hallogen Watt:</td>
                                                    <td><strong><?php echo $cartrow['halogenWatt']; ?></strong></td>
                                                </tr>
                                                <tr>
                                                	<td>Socket:</td>
                                                    <td><strong><?php echo $cartrow['socket']; ?></strong></td>
                                                </tr>
                                                <tr>
                                                	<td>Socket:</td>
                                                    <td><strong><?php echo $cartrow['socket']; ?></strong></td>
                                                </tr>
                                                <tr>
                                                	<td>Beam Intensity:</td>
                                                    <td><strong><?php echo $cartrow['halogenWatt']; ?></strong></td>
                                                </tr>
                                                <tr>
                                                	<td>Beam Angle:</td>
                                                    <td><strong><?php echo $cartrow['beamAngle']; ?></strong></td>
                                                </tr>
                                                <tr>
                                                	<td>Lifetime:</td>
                                                    <td><strong><?php echo $cartrow['lifetime']; ?></strong></td>
                                                </tr>
                                                <tr>
                                                	<td>Dimmable:</td>
                                                    <td><strong><?php echo $cartrow['dimmable']; ?></strong></td>
                                                </tr>
                                                <tr>
                                                	<td>Lumen Output:</td>
                                                    <td><strong><?php echo $cartrow['lumenOutput']; ?></strong></td>
                                                </tr>
                                                <tr>
                                                	<td>Efficacy:</td>
                                                    <td><strong><?php echo $cartrow['efficacy']; ?></strong></td>
                                                </tr>
                                                <tr>
                                                	<td>Units Per Pack:</td>
                                                    <td><strong><?php echo $cartrow['unitsPerPack']; ?></strong></td>
                                                </tr>
                                                <tr>
                                                	<td>Color Rendering:</td>
                                                    <td><strong><?php echo $cartrow['colorRendering']; ?></strong></td>
                                                </tr>
                                                <tr>
                                                	<td>Color Temp:</td>
                                                    <td><strong><?php echo $cartrow['colorTemp']; ?></strong></td>
                                                </tr>
                                                 	<td>NC:</td>
                                                    <td><strong><?php echo $cartrow['NC']; ?></strong></td>
                                                </tr>
                                             </table> 
										</div>
										<div class="clear"></div>
								</div>
								<div class="modal-footer">
									<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
								</div>
						   </div>
						   </div>
                    
                    
                    </td>
                    <td><?php echo $cartrow['itemDesc']; ?></td>
                    <td><input type="text" name="item_<?php echo $cartrow['itemId']; ?>_qty_edit" value="<?php echo @$_SESSION['item_'.$cartrow['itemId'].'_qty']; ?>" style="width:30px; text-align:right;" /></td>
                    <?php if(@$_SESSION['salesName']){ ?><td><?php echo $cartrow['itemPrice']; ?></td><?php } ?>
                    <?php if(@$_SESSION['salesName']){ ?><td><?php echo number_format($cartrow['itemPrice']*@$_SESSION['item_'.$cartrow['itemId'].'_qty'], 2, ".", ","); ?></td><?php } ?>
                </tr>
                <?php 
						}
					}
				?>
               
            </table>
            
            
            <div align="right" style="padding:15px 0px;">
            	<div style="float:left;"><input type="submit" name="updatecart" class="btn btn-primary" value="Update Cart" />&nbsp;
                <input type="submit" name="clearcart" class="btn btn-warning" value="Clear Cart" />&nbsp;
                <?php
					if(@$_SESSION['req_qses']){
				?>
                	<a href="requotedone.php" class="btn">Request Quote</a>
                <?php	
					}else{
				?>
                	<a href="identifyCustomer.php" class="btn"><i class="icon-ok-circle"></i> Request</a>
                <?php
					}
				?>
                </div>
                
            	<?php if(@$_SESSION['salesName']){ ?><strong style="float:right; font-size:18px;">Total:&nbsp;&nbsp;AED <?php echo  number_format($total, 2, ".", ","); ?></strong><?php } ?>
            	<div class="clear"></div>
            </div>
            </form>
        </div>
<?php
	}else{
?>
	<div align="center">
    	<h2 style="border-bottom:none;">Your cart is empty</h2>
    </div>
<?php	
	}
}



?>